package entity

import (
	"encoding/json"
	"time"
)

// 基金活动(爵位活动)
type ActivityPeerage struct {
	RoleID    int32  `gorm:"type:int;primary_key"`
	Open      bool   // 是否冲了1000钻石
	Data      []byte `gorm:"type:jsonb"`
	CreatedAt time.Time
	UpdatedAt time.Time

	data map[int32]bool `sql:"-"`
}

func (e *ActivityPeerage) AfterFind() error {

	e.data = make(map[int32]bool)
	return json.Unmarshal(e.Data, &e.data)
}

func (e *ActivityPeerage) BeforeSave() (err error) {

	if e.data == nil {
		e.data = make(map[int32]bool)
	}

	e.Data, err = json.Marshal(e.data)
	return err
}

func (e *ActivityPeerage) BeforeUpdate() (err error) {

	if e.data == nil {
		e.data = make(map[int32]bool)
	}

	e.Data, err = json.Marshal(e.data)
	return err
}

func (e *ActivityPeerage) Get(peerage int32) bool {
	return e.data[peerage]
}

func (e *ActivityPeerage) Set(peerage int32) {
	e.data[peerage] = true
}
